<?php
// +----------------------------------------------------------------------
// | ThinkSNS
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.thinksns.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: nonant <nonant@thinksns.com>
// +----------------------------------------------------------------------
//

//动态服务
class FeedService extends Service{
	public function __construct($data) {
		//2.服务初始化
		$this->run();
	}


	//运行服务，系统服务自动运行
	public function run(){

	}


	/**
	 +----------------------------------------------------------
	 * Description 添加动态
	 +----------------------------------------------------------
	 * @author Nonant nonant@thinksns.com
	 +----------------------------------------------------------
	 * @param String     $type   动态类型
	 * @param String     $title  动态标题
	 * @param String     $body   动态内容
	 +----------------------------------------------------------
	 * @return int 结果
	 +----------------------------------------------------------
	 * Create at  2010-1-19 下午06:48:41
	 +----------------------------------------------------------
	 */
	public function put($type,$title,$body,$uid='',$uname='',$fid=0,$display=0,$tid){
        $title = $this->_filtrateArr($title);
        $body = $this->_filtrateArr($body);
		$uid = ($uid)?$uid:$_SESSION['mid'];
		$lang = getLang();
		$_template = require SITE_PATH.'/public/language/'.getLang().'/feeds.php';
		if(in_array($type,D('SpaceSetting','home')->get_User_Setting($uid,'feed')) OR $_template[$type]['private']==true){
			$app = explode('_',$type);
			$data['uid']        = $uid;
			$data['type']       = $type;
			$data['target_id']		= $tid;
			$data['title']      = serialize($title);
			$data['body']       = serialize($body);
			$data['fid']        = $fid;
			$data['appname']    = $app[0];
			$data['display']    = $display;  //显示控制 0全局显示 1权内部显示 2仅外部显示
			$data['cTime']      = time();
			return M('Feed')->data($data)->add();
		}
	}
	//删除动态
	public function move($type,$tid){
		$data['type']		= $type;
		$data['target_id']	= $tid;
		return M('Feed')->where($data)->delete();
	}
	//修改动态
	public function change($type,$tid,$title){
		$data['type']		= $type;
		$data['target_id']	= $tid;
		$map['title'] = serialize($title);
		return M('Feed')->where($data)->data($map)->save();
	}
	//过滤 韦心红 2010-7-1
	public function _filtrateArr($value){
		if(is_numeric($value)){
			return $value;
		}
				
		if(is_array($value)){
			foreach ($value as &$v){
				$v = $this->_filtrateArr($v);
			}
			return $value;
		}
	
		return 	h(str_replace(array('&nbsp;','&amp;nbsp;'),'',$value));
	}

	/**
	 +----------------------------------------------------------
	 * Description 添加忽略动态
	 +----------------------------------------------------------
	 * @author Nonant nonant@thinksns.com
	 +----------------------------------------------------------
	 * @param int $id 动态ID
	 +----------------------------------------------------------
	 * @return boolen
	 +----------------------------------------------------------
	 * Create at  2010-2-25 上午11:26:54
	 +----------------------------------------------------------
	 */
	public function ignore($id,$uid){
		$uid = ($uid)?$uid:$_SESSION['mid'];
		$map['uid']    = $uid;
		$map['feedid'] = $id;
		return M('feed_ignore')->add($map);
	}

	/**
	 +----------------------------------------------------------
	 * Description 搜索指定条件下的结果，并返回最终的模板渲染结果
	 +----------------------------------------------------------
	 * @author Nonant nonant@thinksns.com
	 +----------------------------------------------------------
	 * @param int $uid 用户ID
	 +----------------------------------------------------------
	 * @param int $limit 动态数目
	 +----------------------------------------------------------
	 * @param boolen $ignore 是否过滤忽略动态
	 +----------------------------------------------------------
	 * @return array
	 +----------------------------------------------------------
	 * Create at  2010-1-19 下午06:46:54
	 +----------------------------------------------------------
	 */
	public function get($map,$limit=10,$ignore=false,$private=0,$title_template=false,$nopage=true){
		//if($uid) $map['uid'] = $uid;
		if($ignore){
			$ignorelist = M('feed_ignore')->where('uid='.$_SESSION['mid'])->field('feedid')->findall();
			$ignorelist = getSubByKey($ignorelist,'feedid');
			if($ignorelist) $map['id'] = array('NOT IN',$ignorelist);
		}
		
		
		if($nopage){
			$feedlist['data'] = M('Feed')->where($map)->order('cTime DESC')->field('id,title,uid,body,cTime,type,fid,appname,cache_en,cache_cn')->limit($limit)->findall();
				//取出用户名
			 foreach($feedlist['data'] as $value){
				$user[] = $value['uid'];
			 }
		}else{
	    	$feedlist = M('Feed')->where($map)->order('cTime DESC')->field('id,title,uid,body,cTime,type,fid,appname,cache_en,cache_cn')->findpage($limit);
			 foreach($feedlist['data'] as $value){
				$user[] = $value['uid'];
			 }
		}
		$user = array_unique($user);
		$userMap['uid'] = array('in',$user);
		$userDao = model('User');
		$user = $userDao->where($userMap)->field('uid,fullname')->findAll();
		$feedlist['data'] = arrayJoin($feedlist['data'],$user,'uid');
		foreach ($feedlist['data']  as $key=>$value){
			$parseData = $this->__paseTemplate($value,$private,$title_template);
			
			$feedlist['data'] [$key]['id']       = $value['id'];
			$feedlist['data'] [$key]['title']    = $parseData['title'];
			$feedlist['data'] [$key]['body']     = $parseData['body'];
			$feedlist['data'] [$key]['comment']  = $parseData['comment'];

		}
		return $feedlist;
	}

	/**
	 +----------------------------------------------------------
	 * Description 解析动态模版标签
	 +----------------------------------------------------------
	 * @author Nonant nonant@thinksns.com & Sam penglingjun@thinksns.com修改
	 +----------------------------------------------------------
	 * @param string $type 动态类型
	 * @param array  $data 动态的原始数据
	 +----------------------------------------------------------
	 * @return array
	 +----------------------------------------------------------
	 * Create at  2010-1-19 下午06:44:46
	 +----------------------------------------------------------
	 */
	protected function __paseTemplate($aData,$private,$title_template){
		$lang = ( getLang()=='en')?'en':'cn';
		if( $aData["cache_$lang"] && false){     //存在缓存则只接读缓存
			$paseDate = unserialize( $aData["cache_$lang"] );
		}else{
			$_title = unserialize($aData['title']);
			$_body  = unserialize($aData['body']);

			$_type  = $aData['type'];
			// 自定义变量替换
			$app_url      = SITE_URL."/index.php?app=".$aData['appname'];
			$actor_name   = $aData['fullname'];// 站点公共目录
			$actor        = "<a style='white-space: nowrap;' href='".U('space/View/index',array('uid'=>$aData["uid"]))."'>".$actor_name.'</a>';
			$actor_uid    = $aData['uid'];// 站点公共目录
			$_var = "";
	
	
			if($private){
				//$_title = ereg_replace('\<private\>.*\</private\>',"",$_title);
	
			}else{
				if($aData['fid']){  //解析相关应用的链接地址
					$APPLINK = $this->getAppLink( $aData['appname'] ,$aData['fid'] );
				}
			}
			
			if(is_array($_title)){
				foreach ($_title as $k=>$v){
					$$k = $v;
					$_var .= "\${$k} = \"{$v}\";";

				}
			}
	
				
			if(is_array($_body)){
				foreach ($_body as $k=>$v){
					$$k = $v;
					$_var .= "\${$k} = \"{$v}\";";
				}
			}
			
			$_template = require SITE_PATH.'/public/language/'.getLang().'/feeds.php';
			//渲染模板内容
			$paseDate['comment_type']         = $_template[$_type]['comment_type'];
			$paseDate['body']                 = $_template[$_type]['body'];
			$paseDate['title']['common']      = $_template[$_type]['title'];
			if($_template[$_type]['title_template']){ 
				$paseDate['title']['template']    = $_template[$_type]['title_template'];
			}
			
			if(empty($aData['fid']) || ($aData['fid'] && $APPLINK)){
				M('feed')->setField('cache_'.$lang,serialize($paseDate),'id='.$aData['id']);   //生成模板缓存
			}
		}
			
		$aData['body']     = ($private)?ereg_replace('\<private\>.*\</private\>',"",$paseDate['body'] ):$paseDate['body'] ;
		
		if($title_template){
			$aData['title']    = $paseDate['title']['template'];
		}else{
			$aData['title']    = ($private)?ereg_replace('\<private\>.*\</private\>',"",$paseDate['title']['common']):$paseDate['title']['common'];
		}
		$aData['comment']  = ($paseDate['comment_type'])?W('Comment',array('type'=>$paseDate['comment_type'],'admin'=>$aData['uid'],'fid'=>$aData['fid']),true):'';
		return $aData;
	}

	private function getAppLink($appname,$appid){
		if($appname=='group'){
			$html = M('group_x')->where('gid='.$appid)->field('name')->find();
			$html = "<a href=".U('group/Core/index',array('gid'=>$appid)).">".$html['name']."</a>";
		}
		return $html;
	}



	//启动服务，未编码
	public function _start(){
		return true;
	}

	//停止服务，未编码
	public function _stop(){
		return true;
	}

	//安装服务，未编码
	public function _install(){
		return true;
	}

	//卸载服务，未编码
	public function _uninstall(){
		return true;
	}
}
?>